    package com.example.demo.controller;




    import com.example.demo.entity.StudentInfo;
    import com.example.demo.entity.Users;
    import com.example.demo.mapper.StudentMapper;
    import com.example.demo.service.StudentInfoService;
    import com.example.demo.service.UserService;

    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.stereotype.Controller;

    import org.springframework.ui.Model;
    import org.springframework.web.bind.annotation.*;

    import javax.servlet.http.HttpSession;
    import java.util.List;

    @Controller
    public class StudentController {
        @Autowired
        private StudentMapper studentMapper;
        @Autowired
        private UserService userService;
        @Autowired
        private  StudentInfoService studentInfoService;

        @RequestMapping("/")
        public String select(Model model){
            List<StudentInfo> getallstudent =studentInfoService.getallstudentinfo();
            model.addAttribute("Admin",getallstudent);
            return "new";
        }
        @RequestMapping("/tologin")
        public String gologin(){
            return  "login";
        }
        @RequestMapping("/login")
        public String login(Users users, Model model, HttpSession session) {
            Users getuserbyname = userService.getuserbyname(users.getUsername());
            if (getuserbyname == null) {
                model.addAttribute("msg", "无此用户");
                return "login";
            } else if (users.getPassword().equals(getuserbyname.getPassword())) {
                model.addAttribute("msg", "密码错误");
                return "login";
            } else {
                return "redirect:/";
            }
        }

        @RequestMapping("/toregister")
        public String toregister(){
            return "register";
        }
        @RequestMapping("/register")
        public String register(Users users, Model model){
            Users getuserbyname = userService.getuserbyname(users.getUsername());
            if (getuserbyname!=null){
                model.addAttribute("msg","用户名已存在");
                return "register";
            }
            userService.insertuser(users);
            return "redirect:/";
        }



        @RequestMapping("/student/{id}")
        public StudentInfo getUserByid(@PathVariable int id){
                   StudentInfo studentInfo=studentInfoService.getstudentinfo(id);
                   return  studentInfo;

        }
        @RequestMapping("/toinsert")
        public String goinsert(){
            return  "insert";
        }
        @RequestMapping("/insert")
        public String insert(StudentInfo studentInfo) {
          int i= studentInfoService.insertstudentinfo(studentInfo);
          if (i>0){
              System.out.println("新增成功");
          }

            return "redirect:/";
        }

        @RequestMapping("/delete/{id}")
        public String deletestudent(@PathVariable int id){
       studentInfoService.deletestudentinfo(id);
            return "redirect:/";
        }


    }
